<template>
  <div>
    <!-- 面包屑区域 开始 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面包屑区域 结束 -->
    <!-- 卡片区域 开始 -->
    <el-card class="box-card">
      <el-button type="primary">添加角色</el-button>
      <!-- 表格区域 开始 -->
      <el-table :data="rolesList" style="width: 100%">
        <el-table-column type="expand">
          <template v-slot="{ row }">
            <!-- 先根据一级权限渲染出el-row也就是一行 开始-->
            <el-row
              v-for="item1 in row.children"
              :key="item1.id"
              class="el-row1"
            >
              <!-- el-col不指定span的值默认为24 -->
              <el-col :span="6">
                <el-tag>{{ item1.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="18">
                <!-- 每个二级权限也是单独成行 开始 -->
                <el-row
                  class="el-row2"
                  v-for="item2 in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag type="success">{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 每个三级权限就是一个单独的tag就行 开始 -->
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      type="warning"
                      closable
                      @close="removeRights(row, item3.id)"
                      >{{ item3.authName }}</el-tag
                    >
                    <!-- 每个三级权限就是一个单独的tag就行 结束 -->
                  </el-col>
                </el-row>
                <!-- 每个二级权限也是单独成行 结束 -->
              </el-col>
            </el-row>
            <!-- 先根据一级权限渲染出el-row也就是一行 结束-->
          </template>
        </el-table-column>
        <el-table-column type="index" width="50" label="#"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="showRightsDialog(row)"
              >分配</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格区域 结束 -->
    </el-card>
    <!-- 卡片区域 结束 -->
    <!-- 分配权限对话框 开始 -->
    <el-dialog title="分配权限" :visible.sync="rightsDialogVisible" width="50%" @close="rightsDialogClose" v-if="rightsDialogVisible">
      <!-- default-expand-all控制所有节点是否展开  -->
      <el-tree
        :data="rightsTree"
        :props="defaultProps"
        @node-click="handleNodeClick"
        default-expand-all
        show-checkbox
        :default-checked-keys="defCheckKey"
        node-key="id"
        ref="tree"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRights"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 分配权限对话框 结束 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [],
      rightsDialogVisible: false,
      rightsTree: [],
      // 树形控件的控制对象
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      // 默认选中key的数组
      defCheckKey: [],
      // 点击点击分配权限对话框确定按钮后提交后台的角色id
      roleId: ''
    }
  },

  created() {
    this.getRolesList()
  },

  methods: {
    // 获取角色列表
    async getRolesList() {
      const res = await this.$axios.get('/roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色列表失败')
      }
      this.rolesList = res.data
      // console.log(this.rolesList)
    },
    // 关闭三级权限
    removeRights(row, rightId) {
      const { id: roleId } = row
      console.log(roleId, rightId)
      this.$confirm('此操作将删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$axios.delete(
            `roles/${roleId}/rights/${rightId}`
          )
          if (res.meta.status !== 200) {
            return this.$message.error('删除权限失败')
          }
          row.children = res.data
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          console.log(2)
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 展示分配权限对话框
    async showRightsDialog(row) {
      this.roleId = row.id
      this.rightsDialogVisible = true
      this.getThirdRights(row)
      const res = await this.$axios.get('/rights/tree')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色列表失败')
      }
      this.rightsTree = res.data
    },
    // 递归获取第三级权限的id
    getThirdRights(data) {
      if (!data.children) {
        this.defCheckKey.push(data.id)
        return
      }
      data.children.forEach(item => {
        this.getThirdRights(item)
      })
    },
    // 分配权限对话框关闭时的回调
    rightsDialogClose() {
      this.defCheckKey = []
    },
    // 点击分配权限对话框确定按钮
    async setRights() {
      const allKeys = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]
      const res = await this.$axios.post(`roles/${this.roleId}/rights`, {
        rids: allKeys.join(',')
      })
      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败')
      }
      this.rightsDialogVisible = false
      this.getRolesList()
      this.$message.success('分配权限成功')
    },
    handleNodeClick(data) {
      console.log(data)
    }
  }
}
</script>

<style scoped lang='less'>
.el-tag {
  margin: 8px;
}
.el-row1 {
  border-top: 1px solid #eee;
}
.el-row1:nth-last-child(1) {
  border-bottom: 1px solid #eee;
}
.el-row2 + .el-row2 {
  border-top: 1px solid #eee;
}
.el-row {
  display: flex;
  align-items: center;
}
</style>
